/*
 ============================================================================
 Name        : bill-tts-agent-client-example.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <curl.h>
#include <stddef.h>
#include <stdio.h>

int main(void) {
	example1();
}
void example1(){
  char buffer[128] = { 0 };
    long http_code;
    char *content_type = NULL, *url = NULL;
    size_t url_len = 0;

    CURL *curl = NULL;
    struct curl_slist *headers = NULL;

    // invoke the api to download the text wave data
    curl = curl_easy_init();
    if (curl == NULL) {
      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "yytts failed to init the curl\n");
      curl_global_cleanup();
      return SWITCH_STATUS_FALSE;
    }

    // make the request url api_base + ?text=${text}&voice=${voice}
    url_len = strlen(globals.api_base);
    url_len += 37;                          // length for ?app_key=&text=&voice=&audio_type=wav
    url_len += strlen(globals.app_key);     // bytes for app_key
    url_len += strlen(yytts->voice);
    url = (char *) switch_core_alloc(yytts->pool, url_len + 1);
    strcat(url, globals.api_base);
    strcat(url, "?app_key=");
    strcat(url, globals.app_key);
    strcat(url, "&text=");
    strcat(url, text);
    strcat(url, "&voice=");
    strcat(url, yytts->voice);
    strcat(url, "&audio_type=");
    strcat(url, globals.audio_type);
    /// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "yytts.feed_tts url=%s\n", url);

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
    curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 15000);          // 15 seconds
    curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, 36000);   // 10 hours for DNS cache

    // initialized the audio_buffer and set the recv callback
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wave_recv_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, yytts);

    // make the request header
    snprintf(buffer, sizeof(buffer), "Content-Type: application/json");
    headers = curl_slist_append(headers, buffer);
    snprintf(buffer, sizeof(buffer), "User-Agent: mod_yytts/freeswitch/0.1 yuanyu.ai");
    headers = curl_slist_append(headers, buffer);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    // perform the request
    curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
    curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &content_type);
    curl_slist_free_all(headers);
    if (http_code != 200) {
      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "yytts feed failed to perform the request\n");
      curl_easy_cleanup(curl);
      return SWITCH_STATUS_FALSE;
    }

    if (content_type == NULL || strstr(content_type, "audio/") == NULL) {
      curl_easy_cleanup(curl);
      curl_global_cleanup();
      return SWITCH_STATUS_FALSE;
    }

    curl_easy_cleanup(curl);
    curl_global_cleanup();
    return SWITCH_STATUS_SUCCESS;
}
